package com.bingbing.back.controller;

import com.bingbing.back.bean.SessionList;
import com.bingbing.back.bean.User;
import com.bingbing.back.common.bean.AjaxResult;
import com.bingbing.back.mapper.ConsultInfoMapper;
import com.bingbing.back.mapper.SeesionListMapper;
import com.bingbing.back.mapper.UserMapper;
import com.bingbing.back.po.ConsultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin
@RestController
public class SessionController {

    @Autowired(required=false)
    private UserMapper userMapper;

    @Autowired(required=false)
    private SeesionListMapper seesionListMapper;

    @Autowired(required=false)
    private ConsultInfoMapper consultInfoMapper;

    /**
     * 已建立会话
     */
    @GetMapping("/sessionList/already")
    public AjaxResult<?> sessionListAlready(@RequestParam Integer id){
        List<SessionList> sessionLists = seesionListMapper.selectByUserId(id);
        return  AjaxResult.success(sessionLists);
    }

    @GetMapping("/sessionList/searchConsult")
    public AjaxResult<?> searchConsult(@RequestParam String name){
        String searchConsultName = consultInfoMapper.selectByConsultName(name);
        return  AjaxResult.success(searchConsultName);
    }

    // 可建立会话的用户
    @GetMapping("/sessionList/not")
    public AjaxResult<?> sessionListNot(@RequestParam Integer id){
        List<Integer> list = seesionListMapper.selectUserIdByUserId(id);//筛选出已经建立会话的
        list.add(id);

        List<ConsultInfo> cloudList = consultInfoMapper.getCloudList(list);//not in 筛选出未建立会话和自己
        System.out.println(list);
        System.out.println(cloudList);
        return AjaxResult.success(cloudList);
    }

    @GetMapping("/sessionAllList/not")
    public AjaxResult<?> sessionAllList(@RequestParam Integer id){
        List<Integer> list = seesionListMapper.selectUserIdByUserId(id);//筛选出已经建立会话的
        list.add(id);

        List<User> cloudList = userMapper.getCloudList(list);//not in 筛选出未建立会话和自己
        System.out.println(list);
        System.out.println(cloudList);
        return AjaxResult.success(cloudList);
    }

    // 创建会话
    @GetMapping("/createSession")
    public AjaxResult<?> createSession(@RequestParam Integer id,@RequestParam String userIdName){
        SessionList sessionList = new SessionList();
        sessionList.setUserId(id);
        sessionList.setUnReadCount(0);
        sessionList.setUserIdName(userIdName);
        List<ConsultInfo> ConsultInfoList = consultInfoMapper.selectAllConsult();
        System.out.println(ConsultInfoList);
        for (ConsultInfo info : ConsultInfoList) {
            sessionList.setListName(info.getConsultName());
            sessionList.setToUserId(info.getConsultId());
            seesionListMapper.insert(sessionList);
        }
        return AjaxResult.success();
    }

    @GetMapping("/createAllSession")
    public AjaxResult<?> createAllSession(@RequestParam Integer id,@RequestParam String userIdName, @RequestParam Integer toUserId, @RequestParam String toUserName){
        SessionList sessionList = new SessionList();
        sessionList.setUserId(id);
        sessionList.setUnReadCount(0);
        sessionList.setListName(toUserName);
        sessionList.setUserIdName(userIdName);
        sessionList.setToUserId(toUserId);
        seesionListMapper.insert(sessionList);
        // 判断对方和我建立会话
        Integer integer = seesionListMapper.selectIdByUser(toUserId, id);
        if (integer == null || integer <= 0){
            ConsultInfo consultInfo = consultInfoMapper.selectByPrimaryKey(id);
            sessionList.setUserId(toUserId);
            sessionList.setToUserId(id);
            sessionList.setUserIdName(toUserName);
            sessionList.setListName(consultInfo.getConsultName());
            seesionListMapper.insert(sessionList);
        }
        return AjaxResult.success();
    }
    @GetMapping("/sessionList/select")
    public AjaxResult<?> sessionListSelect(@RequestParam Integer id){
        String list_name = seesionListMapper.selectIdBySession(id);
        return  AjaxResult.success(list_name);
    }

    // 删除会话
    @GetMapping("/delSession")
    public AjaxResult<?> delSession(@RequestParam Integer sessionId){
        seesionListMapper.deleteByPrimaryKey(sessionId);
        return AjaxResult.success();
    }

}
